#+
#
#-
import os
import glob
import tarfile

pathRow = [
#  Equador
'009060',
'010060',
#  Guiana
'230057',
'230058',
'230059',
'231057',
'231058',
'231059'
# Para-Amapa
# '230060',
# '230061',
# '229061',
# '229060',
# '229059',
# '228058',
# '228059',
# '228060',
# '228061',
# '227061',
# '227060',
# '227059',
# '227058',
# '226057',
# '226058',
# '226059',
# '226060',
# '225059'
]

inputDir  = "H:\DADOS\TEMP"
outputDir = "H:\DADOS\IMAGES\TEMP"

for j in range(len(pathRow)):
    tarGzFileList = glob.glob(inputDir +'/*'+ pathRow[j] + '*.tar.gz')
    print "##", pathRow[j] 
    
    for tarGzFile in tarGzFileList:
        splited = tarGzFile.split("\\")
        splited = splited[len(splited)-1].split(".")
        
        folderName = splited[0]
        
        untaredFilesDir = outputDir +'\\'+ pathRow[j] +'\\'+ folderName
        if not os.path.exists(untaredFilesDir):
            os.makedirs(untaredFilesDir)
        
        os.chdir(untaredFilesDir)
        
        print 'Uncompressing file...' + tarGzFile
        try:
            tar = tarfile.TarFile.open(tarGzFile, 'r')
            tar.extractall()
            tar.close()
        except:
            print "The file cannot be uncompressed! "  + tarGzFile

print "Finished!"
